// pages/address-add/address-add.js
const common = require('../../utils/common.js');
const api = require('../../api/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shifou_moren: '',
    region: ['省份、城市、县区'],
    addr_id: '',
    shouhuo_name: '',
    shouhuo_tel: '',
    shouhuo_detailarea: '',
    is_default: '',
    checked: true,
    isDefault: 0
  },
  onChange({
    detail
  }) {
    // 需要手动对 checked 状态进行更新
    this.setData({
      checked: detail
    });
  },

  bindinputShouhuo_name(event) {
    console.log(event)
    this.setData({
      shouhuo_name: event.detail.value
    });
  },
  bindinputShouhuo_tel(event) {
    this.setData({
      shouhuo_tel: event.detail.value
    });
  },
  bindinputAddress(event) {
    this.setData({
      address: event.detail.value
    });
  },
  shouhuo_detailarea(event) {
    this.setData({
      address: event.detail.value
    });
    console.log(this.data.address)
  },

  bindIsDefault() {
    let isDefault = this.data.is_default;
    if (isDefault == 1) {
      isDefault = 0;
    } else {
      isDefault = 1;
    }
    this.setData({
      isDefault: isDefault
    });
  },

  bindRegionChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value
    })
  },

  //先获取到地址的信息
  get_addrDetail() {
    let that = this;
    let addr_id = that.data.addr_id
    common.request(api.get_addrDetail, {
      addr_id: addr_id
    }, 'post').then((res) => {
      if (res.status == 1) {
        that.setData({
          shouhuo_name: res.oneaddr.shouhuo_name,
          shouhuo_tel: res.oneaddr.shouhuo_tel,
          region: res.oneaddr.shouhuo_area,
          shouhuo_detailarea: res.oneaddr.shouhuo_detailarea,
          is_default: res.oneaddr.is_default,
        })
        console.log(res)
      }
    })
  },

  //编辑地址
  Edit_address() {
    let that = this;
    let checked = that.data.checked
    let isDefault = ''
    if (checked) {
      isDefault = 1
    } else {
      isDefault = 0
    }
    let data = {
      addr_id: that.data.addr_id,
      shouhuo_name: that.data.shouhuo_name,
      shouhuo_tel: that.data.shouhuo_tel,
      shouhuo_area: that.data.region,
      shouhuo_detailarea: that.data.shouhuo_detailarea,
      is_default: isDefault
    }
    let tel = that.data.shouhuo_tel
    if (!(/^1[345678]\d{9}$/.test(tel))) {
      wx.showToast({
        title: '请正确填写手机号',
        icon: 'waring',
        duration: 2000,
      });
      return false;
    }
    common.request(api.Edit_address, data, 'post').then((res) => {
      if (res.status == 1) {
        common.showSuccessToast(res.errmsg);
        setTimeout(function () {
          wx.navigateBack({
            changed: true
          });
        }, 1500)
      }
    })
  },

  //设置默认
  setMoren() {
    this.setData({
      shifou_moren: 0
    })
  },

  //取消默认
  quxiaoMoren() {
    this.setData({
      shifou_moren: 1
    })
  },

  shouhuo_name(e) {
    let shouhuo_name = e.detail.value
    this.setData({
      shouhuo_name: shouhuo_name
    })
  },
  shouhuo_tel(e) {
    let shouhuo_tel = e.detail.value;
    this.setData({
      shouhuo_tel: shouhuo_tel
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      addr_id: options.addr_id
    })
    this.get_addrDetail()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})